//
//  WebView.swift
//  MD5
//
//  Created by 郑东喜 on 2016/11/22.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit
import WebKit


class WebView: UIViewController,WKUIDelegate,WKScriptMessageHandler {

    
    var webView: WKWebView!
    

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        var webView = WKWebView()
        
        //wkwebview相关配置
        
        
        //创建并配置WKWebView的相关参数
        //1.WKWebViewConfiguration:是WKWebView初始化时的配置类，里面存放着初始化WK的一系列属性；
        //2.WKUserContentController:为JS提供了一个发送消息的通道并且可以向页面注入JS的类，WKUserContentController对象可以添加多个scriptMessageHandler；
        //3.addScriptMessageHandler:name:有两个参数，第一个参数是userContentController的代理对象，第二个参数是JS里发送postMessage的对象。添加一个脚本消息的处理器,同时需要在JS中添加，window.webkit.messageHandlers.<name>.postMessage(<messageBody>)才能起作用。
        let configuration = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        
        userContentController.add(self as WKScriptMessageHandler, name: "shareWeixinInfo")
        
        
        configuration.userContentController = userContentController
        
        let preferences = WKPreferences()
        preferences.javaScriptCanOpenWindowsAutomatically = true
        preferences.minimumFontSize = 10.0
        configuration.preferences = preferences
        
        webView.uiDelegate = self
        
        webView = WKWebView.init(frame: view.bounds, configuration: configuration)
        
        //本地加载
        let htmlPath = Bundle.main.path(forResource: "WKWebViewMessageHandler", ofType: "html")
        
        
        do {
            let fileURL = try NSString.init(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
            let baseURL = NSURL.fileURL(withPath: htmlPath! as String)
            webView.loadHTMLString(fileURL as String, baseURL: baseURL)
        } catch  {
            
        }
        

        view.addSubview(webView)

    }
    
    /**
     *  JS 调用 swift 时 webview 会调用此方法
     *
     *  @param userContentController  webview中配置的userContentController 信息
     *  @param message                JS执行传递的消息
     */

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        print(message.body)
        let msg = message.name
        
        if  msg == "shareWeixinInfo" {
            print("shareWeixinInfo")
            
            let artController = UIAlertController(title: "测试", message: message.body as? String, preferredStyle: .alert)
            
            artController.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            self.present(artController, animated: true, completion: nil)
        }
    }
    
    
}

// MARK:- 设置表格头
extension WebView {
    func setHead() -> Void {
        
    }
}


extension WebView {
    func toBack() {
        if self.webView.canGoBack {
            self.webView.goBack()
            
        }
    }
    
    
    //获取
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        print(self.webView.title as Any)
        self.navigationItem.title = self.webView.title
    }
    
    
    //
    
    func toFroward() -> Void {
        if self.webView.canGoForward {
            self.webView.goForward()
        }
    }
    

}
